package com.appcenter.application.service;

import com.appcenter.application.dto.jobcategorys.JobCategoryDto;
import com.appcenter.application.dto.jobs.JobDto;
import com.appcenter.application.expand.ObjectFinal;
import com.appcenter.application.input.Job.*;
import com.appcenter.application.input.app.GetAppsInput;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.JobCategorysMapper;
import com.appcenter.application.mapper.JobsMapper;
import com.appcenter.application.model.Apps;
import com.appcenter.application.model.JobCategorys;
import com.appcenter.application.model.Jobs;
import com.appcenter.application.output.Job.*;
import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

import static org.apache.http.util.TextUtils.isBlank;

@Service
public class JobService {
    @Autowired
    JobsMapper jobsMapper;

    @Autowired
    AppsMapper appsMapper;

    @Autowired
    JobCategorysMapper jobCategorysMapper;

    @Autowired
    ObjectFinal objectFinal;

//    @Transactional
//    @Cacheable(value="basedata",keyGenerator = "ObjectKey")
    /**
     *  创建人:Coke
     *  备注: 加载岗位信息
     *  时间: 2019/4/26 13:53 
     *  参数: 
     */
    public PageLoadOutput PageLoad(PageLoadInput input) throws Exception {
        try {
            PageLoadOutput output = new PageLoadOutput();
            Map<String, JobCategoryDto> parentJobCategoryDtoDic = new HashMap<String, JobCategoryDto>();

            GetAppsInput getAppsInput=new GetAppsInput();
            //List<Apps> apps = appsMapper.SearchApps(getAppsInput);

            List<JobCategorys> jobCategorys=GetJobCategoryTreeList(null,input.getAppCode());
            List<JobCategoryDto> jobCategoryDtos = new ArrayList<JobCategoryDto>();

            if (jobCategorys != null && jobCategorys.size() > 0) {
                for (int i = 0; i < jobCategorys.size(); i++) {
                    JobCategoryDto jobCategoryDto = new JobCategoryDto();
                    BeanUtils.copyProperties(jobCategorys.get(i), jobCategoryDto);

                    if (jobCategorys.get(i).getParentId() == null || !parentJobCategoryDtoDic.containsKey(jobCategorys.get(i).getParentId())) {
                        jobCategoryDtos.add(jobCategoryDto);
                    } else {
                        JobCategoryDto parentJobCategoryDto = parentJobCategoryDtoDic.get(jobCategoryDto.getParentId());
                        if (parentJobCategoryDto.getChildren() == null) {
                            parentJobCategoryDto.setChildren(new ArrayList<JobCategoryDto>());
                        }
                        parentJobCategoryDto.getChildren().add(jobCategoryDto);
                    }
                    parentJobCategoryDtoDic.put(jobCategoryDto.getId(), jobCategoryDto);
                }
            }
            input.setPenetrate(true);
            input.setJobCategoryId(null);
            GetJobsOutput getJobsOutput=GetJobs(input);

            //output.setApps(apps);
            output.setJobCategorys(jobCategoryDtos);
            output.setItems(getJobsOutput.getItems());
            return output;
        } catch (Exception e) {
            throw new ErrorJsonResult("1010203123", e.toString());
        }
    }
//    @Transactional
//    @Cacheable(value="basedata")
    /**
     *  创建人:Coke
     *  备注: 获取岗位分类
     *  时间: 2019/4/26 13:53 
     *  参数: 
     */
    public List<JobCategorys> GetJobCategoryTreeList(String parentId, String AppCode){
        if(parentId!=null && parentId.length()>0) {

        }
        List<JobCategorys> jobCategorysList=jobCategorysMapper.GetJobCategoryTreeList(AppCode);
        return jobCategorysList;
    }
//    @Transactional
//    @Cacheable(value="basedata",keyGenerator = "ObjectKey")
    /**
     *  创建人:Coke
     *  备注: 获取岗位信息
     *  时间: 2019/4/26 13:54 
     *  参数: 
     */
    public GetJobsOutput GetJobs(GetJobsInput input)  throws Exception {
        GetJobsOutput output=new  GetJobsOutput();
        if (isBlank(input.getAppCode()))
            input.setAppCode(null);
        if (!isBlank(input.getJobCategoryId())) {
            JobCategorys jobCategory=jobCategorysMapper.selectByPrimaryKey(input.getJobCategoryId());
            if (jobCategory!=null) {
                input.setIdPath(jobCategory.getIdPath());
            }
        }
        else{
            input.setJobCategoryId(null);
        }
        input.setVersionNo(objectFinal.getDataEffectiveVersion());
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "Id desc");
        List<JobDto>  ls=jobsMapper.GetJobs(input);
        PageInfo<JobDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<JobDto>(page.getList(), (int) page.getTotal()));
        return  output;
    }

//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 创建岗位
     *  时间: 2019/4/26 13:54 
     *  参数: 
     */
    public CreateJobOutput CreateJob(CreateJobInput input) throws Exception {
        CreateJobOutput output = new CreateJobOutput();
        Jobs job=new Jobs();
        BeanUtils.copyProperties(input.getItem(),job);
        Example exampleJob = new Example(Jobs.class);
        Example.Criteria criteria = exampleJob.createCriteria();
        criteria.andEqualTo("jobCode", input.getItem().getJobCode());
        int countJob = jobsMapper.selectCountByExample(exampleJob);
        if (countJob > 0) {
            throw new ErrorJsonResult("1000031", "分类编号已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        job.setId(uuid);
        job.setCreateTime(new Date());
        job.setCreateUserId(CurrentUser.getUserId());
        job.setDeleted(false);
        job.setValidStatus(true);
       // job.setVersionNo(objectFinal.getDataEffectiveVersion());
        jobsMapper.insertSelective(job);
        output.setItem(job);
        return output;
    }

//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 修改岗位
     *  时间: 2019/4/26 13:54 
     *  参数: 
     */
    public EditJobOutput EditJob(EditJobInput input) throws Exception {
        EditJobOutput output = new EditJobOutput();
        Jobs job=jobsMapper.selectByPrimaryKey(input.getItem().getId());
        job.setJobName(input.getItem().getJobName());
       jobsMapper.updateByPrimaryKey(job);
        output.setItem(job);
        return output;
    }

//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 删除岗位
     *  时间: 2019/4/26 13:55 
     *  参数: 
     */
    public DeleteJobOutput DeleteJob(DeleteJobInput input) throws Exception {
        DeleteJobOutput output = new DeleteJobOutput();
        Jobs job=jobsMapper.selectByPrimaryKey(input.getItem().getId());
        job.setDeleted(true);
        jobsMapper.updateByPrimaryKey(job);
        output.setItem(job);
        return output;
    }
    /**
     *  创建人:Coke
     *  备注: 修改岗位
     *  时间: 2019/4/26 14:06
     *  参数: 
     */
//    @Transactional
    public EditJobOutput EditJobStatus(EditJobInput input) throws Exception {
        EditJobOutput output = new EditJobOutput();
        Jobs job=jobsMapper.selectByPrimaryKey(input.getItem().getId());
        job.setValidStatus(input.getItem().getValidStatus());
        jobsMapper.updateByPrimaryKey(job);
        output.setItem(job);
        return output;
    }

}
